package shop.up.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import shop.up.common.result.Result;
import shop.up.model.product.SkuInfo;
import shop.up.model.product.SpuImage;
import shop.up.model.product.SpuSaleAttr;
import shop.up.product.service.ManageService;

import javax.annotation.Resource;
import java.util.List;

/**
 * sku 控制器
 * @author zzc
 * @date 2023/3/30 14:40
 */
@Api(tags = "sku控制器")
@RestController
@RequestMapping("admin/product")
public class SkuManageController {

    @Resource
    private ManageService manageService;

    /**
     * 根据spuId 查询spuImageList
     * @param spuId
     * @return
     */
    @ApiOperation("根据spuId 查询spuImageList")
    @GetMapping("spuImageList/{spuId}")
    public Result<List<SpuImage>> getSpuImageList(@PathVariable("spuId") Long spuId) {
        List<SpuImage> spuImageList = manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    /**
     * 根据spuId查询SpuSaleAttrList
     * @param spuId
     * @return
     */
    @ApiOperation("根据spuId查询SpuSaleAttrList")
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrList(@PathVariable("spuId") Long spuId) {
        List<SpuSaleAttr> spuSaleAttrList = manageService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    /**
     * 保存sku
     * @param skuInfo
     * @return
     */
    @ApiOperation("保存sku")
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    /**
     * 分页查询sku列表
     * @return
     */
    @ApiOperation("分页查询sku列表")
    @GetMapping("list/{page}/{limit}")
    public Result<IPage<SkuInfo>> getSkuInfoPage(@PathVariable("page") Long page,
                                                 @PathVariable("limit") Long limit) {
        Page<SkuInfo> skuInfoPage = new Page<>(page, limit);
        IPage<SkuInfo> pageResult = manageService.getSkuInfoPage(skuInfoPage);
        return Result.ok(pageResult);
    }

    /**
     * 上架商品
     * @return
     */
    @ApiOperation("上架商品")
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId) {
        manageService.onSale(skuId);
        return Result.ok();
    }

    /**
     * 下架商品
     * @return
     */
    @ApiOperation("下架商品")
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId) {
        manageService.cancelSale(skuId);
        return Result.ok();
    }


}
